package com.study.mike.controller;

import com.qiniu.common.QiniuException;
import com.qiniu.storage.DownloadUrl;
import com.qiniu.util.Auth;
import com.qiniu.util.IOUtils;
import com.study.mike.common.utils.QiniuUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.xmlbeans.impl.common.IOUtil;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.UUID;

/**
 * @author KangHui
 * @Date 2022/7/4 16:55
 */
@RestController
public class QiniuController {
    @Resource
    private QiniuUtil qiniuUtil;

    @PostMapping(value = "/upload")
    public String upload() throws IOException {
        //自定义一个文件
        File myFile = new File("src\\main\\resources\\static\\picture.png");
        FileInputStream fileInputStream = new FileInputStream(myFile);
        MultipartFile file = new MockMultipartFile(myFile.getName(), IOUtils.toByteArray(fileInputStream));
        //文件名称
        String originalFilename = file.getOriginalFilename();
        //将原始名称修改为：唯一文件名称
        String fileName = "book/" + UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(originalFilename, ".");
        //调用文件上传
        String uploadFileName = qiniuUtil.upload(file, fileName);
        if (uploadFileName != null) {
            //上传成功
            return "success";
        }
        return "error";
    }

    @PostMapping("/download")
    public String download() throws UnsupportedEncodingException, QiniuException {

        //1、方式
//        DownloadUrl url = new DownloadUrl("rehol2q7u.hn-bkt.clouddn.com", false, "FlGSlb0ZSqyWWNI4rIsZOGDf733n");
//        // 配置 style
//        // 带有效期
//        //1小时，可以自定义链接过期时间
//        long expireInSeconds = 3600;
//        Auth auth = Auth.create("aMfHrKtJTx-hvDlXpIlEcifMNkWAtXG4A_TuiGj6", "_Gk8xYYMuV4yDkyg7RzM1XG92ok-kY-BJjY0SveJ");
//        String urlString = url.buildURL(auth, expireInSeconds);
//        System.out.println(urlString);
//        return null;

        //2、方式
        String fileName = "picture";
        String domainOfBucket = "rehol2q7u.hn-bkt.clouddn.com";
        String encodedFileName = URLEncoder.encode(fileName, "utf-8").replace("+", "%20");
        String publicUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
        String accessKey = "aMfHrKtJTx-hvDlXpIlEcifMNkWAtXG4A_TuiGj6";
        String secretKey = "_Gk8xYYMuV4yDkyg7RzM1XG92ok-kY-BJjY0SveJ";
        Auth auth = Auth.create(accessKey, secretKey);
        //1小时，可以自定义链接过期时间
        long expireInSeconds = 3600;
        String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
        System.out.println(finalUrl);

        // 3、方式

//        String fileName = "picture";
//        //自己七牛云上的域名
//        String domainOfBucket = "rehol2q7u.hn-bkt.clouddn.com";
//        String encodedFileName = URLEncoder.encode(fileName, "utf-8").replace("+", "%20");
//        String finalUrl = String.format("%s/%s", domainOfBucket, encodedFileName);
//        System.out.println(finalUrl);
        return null;


    }
}
